<!DOCTYPE html>
<html lang=zh>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=5" />
    <meta name="description" content="Servlet1.Servlet获取参数1.1编写页面123456789101112131415&lt;!DOCTYPE html&gt;&lt;html lang&#x3D;&quot;en&quot;&gt;&lt;head&gt;    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;    &lt;title&gt;Title&lt;&#x2F;title&gt;&lt;&#x2F;head">
<meta property="og:type" content="article">
<meta property="og:title" content="servlet">
<meta property="og:url" content="https://mihaoyou.gitee.io/servlet/index.html">
<meta property="og:site_name" content="Cactus">
<meta property="og:description" content="Servlet1.Servlet获取参数1.1编写页面123456789101112131415&lt;!DOCTYPE html&gt;&lt;html lang&#x3D;&quot;en&quot;&gt;&lt;head&gt;    &lt;meta charset&#x3D;&quot;UTF-8&quot;&gt;    &lt;title&gt;Title&lt;&#x2F;title&gt;&lt;&#x2F;head">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://mihaoyou.gitee.io/servlet.assets/cgiarch.gif">
<meta property="og:image" content="https://mihaoyou.gitee.io/servlet.assets/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjkzMTcx,size_1,color_FFFFFF,t_70-169468258611917.png">
<meta property="og:image" content="https://mihaoyou.gitee.io/servlet.assets/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjkzMTcx,size_1,color_FFFFFF,t_70-16946539928691.png">
<meta property="og:image" content="https://mihaoyou.gitee.io/servlet.assets/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjkzMTcx,size_1,color_FFFFFF,t_70.png">
<meta property="og:image" content="https://mihaoyou.gitee.io/servlet.assets/2020071011334452.png">
<meta property="og:image" content="https://mihaoyou.gitee.io/servlet.assets/20200710113504890.png">
<meta property="og:image" content="https://mihaoyou.gitee.io/servlet.assets/2020071011381727.png">
<meta property="og:image" content="https://mihaoyou.gitee.io/servlet.assets/20200710113846116.png">
<meta property="og:image" content="https://mihaoyou.gitee.io/servlet.assets/20200710114011565.png">
<meta property="og:image" content="https://mihaoyou.gitee.io/servlet.assets/2020070915062122.png">
<meta property="og:image" content="https://mihaoyou.gitee.io/servlet.assets/20200709194045973.png">
<meta property="article:published_time" content="2023-09-12T14:06:36.000Z">
<meta property="article:modified_time" content="2023-09-18T14:16:02.203Z">
<meta property="article:author" content="云雪">
<meta property="article:tag" content="servlet">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://mihaoyou.gitee.io/servlet.assets/cgiarch.gif">
    
    
      
        
          <link rel="shortcut icon" href="/images/favicon.ico">
        
      
      
        
          <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
        
      
      
        
          <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
        
      
    
    <!-- title -->
    <title>servlet</title>

    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">


     <!-- 添加到这里！！ -->
     
<script src="/js/switch.js"></script>


    <!-- persian styles -->
    
    <!-- rss -->
    
    
	<!-- mathjax -->
	
<meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="Cactus" type="application/atom+xml">
</head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#" aria-label="目录"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#" aria-label="目录"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" aria-label="顶部" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
        <!--
       --><li><a href="/">首页</a></li><!--
     --><!--
       --><li><a href="/about/">关于</a></li><!--
     --><!--
       --><li><a href="/archives/">归档</a></li><!--
     --><!--
       --><li><a href="/categories/">分类</a></li><!--
     --><!--
       --><li><a href="/search/">搜索</a></li><!--
     -->
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" aria-label="上一篇" href="/ssm/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" aria-label="下一篇" href="/%E7%B3%BB%E7%BB%9F%E6%9E%B6%E6%9E%84%E5%B8%88/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" aria-label="返回顶部" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" aria-label="分享文章" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>

        <li><a class="icon" aria-label="切换主题 " href="#"><i class="fas fa-lightbulb" aria-hidden="true" onmouseover="$('#i-switch').toggle();" onmouseout="$('#i-switch').toggle();" onclick="switchNightMode();return false";></i></a></li>

      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
      <span id="i-switch" class="info" style="display:none;">切换主题</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://mihaoyou.gitee.io/servlet/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://mihaoyou.gitee.io/servlet/&text=servlet"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://mihaoyou.gitee.io/servlet/&title=servlet"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://mihaoyou.gitee.io/servlet/&is_video=false&description=servlet"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=servlet&body=Check out this article: https://mihaoyou.gitee.io/servlet/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://mihaoyou.gitee.io/servlet/&title=servlet"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://mihaoyou.gitee.io/servlet/&title=servlet"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://mihaoyou.gitee.io/servlet/&title=servlet"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://mihaoyou.gitee.io/servlet/&title=servlet"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://mihaoyou.gitee.io/servlet/&name=servlet&description="><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://mihaoyou.gitee.io/servlet/&t=servlet"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Servlet"><span class="toc-number">1.</span> <span class="toc-text">Servlet</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-Servlet%E8%8E%B7%E5%8F%96%E5%8F%82%E6%95%B0"><span class="toc-number">1.1.</span> <span class="toc-text">1.Servlet获取参数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-1%E7%BC%96%E5%86%99%E9%A1%B5%E9%9D%A2"><span class="toc-number">1.1.1.</span> <span class="toc-text">1.1编写页面</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-2%E5%88%9B%E5%BB%BAservlet%E7%B1%BB"><span class="toc-number">1.1.2.</span> <span class="toc-text">1.2创建servlet类</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3%E9%85%8D%E7%BD%AEweb-xml"><span class="toc-number">1.1.3.</span> <span class="toc-text">1.3配置web.xml</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-4%E5%90%AF%E5%8A%A8tomcat"><span class="toc-number">1.1.4.</span> <span class="toc-text">1.4启动tomcat</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%A4%84%E7%90%86%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98"><span class="toc-number">1.2.</span> <span class="toc-text">2.处理请求参数中文乱码问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-Servlet%E7%9A%84%E7%BB%A7%E6%89%BF%E5%85%B3%E7%B3%BB"><span class="toc-number">1.3.</span> <span class="toc-text">3.Servlet的继承关系</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-Servlet%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-number">1.4.</span> <span class="toc-text">4.Servlet生命周期</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-Http%E5%8D%8F%E8%AE%AE"><span class="toc-number">1.5.</span> <span class="toc-text">5.Http协议</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1Http%E5%8D%8F%E8%AE%AE%E7%AE%80%E4%BB%8B"><span class="toc-number">1.5.1.</span> <span class="toc-text">5.1Http协议简介</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2HTTP-%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86"><span class="toc-number">1.5.2.</span> <span class="toc-text">5.2HTTP 工作原理</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-3%E8%AF%B7%E6%B1%82%E6%96%B9%E6%B3%95"><span class="toc-number">1.5.3.</span> <span class="toc-text">5.3请求方法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-4%E8%AF%B7%E6%B1%82%E6%B6%88%E6%81%AF%E5%A4%B4"><span class="toc-number">1.5.4.</span> <span class="toc-text">5.4请求消息头</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-5%E8%AF%B7%E6%B1%82%E4%BD%93"><span class="toc-number">1.5.5.</span> <span class="toc-text">5.5请求体</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-6%E5%93%8D%E5%BA%94"><span class="toc-number">1.5.6.</span> <span class="toc-text">5.6响应</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-7%E7%8A%B6%E6%80%81%E7%A0%81"><span class="toc-number">1.5.7.</span> <span class="toc-text">5.7状态码</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-%E4%BC%9A%E8%AF%9D"><span class="toc-number">1.6.</span> <span class="toc-text">6.会话</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#6-1HTTP%E6%98%AF%E6%97%A0%E7%8A%B6%E6%80%81%E7%9A%84"><span class="toc-number">1.6.1.</span> <span class="toc-text">6.1HTTP是无状态的</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-2%E4%BC%9A%E8%AF%9D%E8%B7%9F%E8%B8%AA%E6%8A%80%E6%9C%AF"><span class="toc-number">1.6.2.</span> <span class="toc-text">6.2会话跟踪技术</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-3session%E4%BF%9D%E5%AD%98%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">1.6.3.</span> <span class="toc-text">6.3session保存作用域</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%86%85%E9%83%A8%E8%BD%AC%E5%8F%91%E4%BB%A5%E5%8F%8A%E5%AE%A2%E6%9C%8D%E7%AB%AF%E9%87%8D%E5%AE%9A%E5%90%91"><span class="toc-number">1.7.</span> <span class="toc-text">7.服务器内部转发以及客服端重定向</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-Thymeleaf%E2%80%93%E8%A7%86%E5%9B%BE%E6%A8%A1%E7%89%88%E6%8A%80%E6%9C%AF"><span class="toc-number">1.8.</span> <span class="toc-text">8.Thymeleaf–视图模版技术</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%A6%E4%B9%A0Thymeleaf%E5%BF%85%E7%9F%A5%E7%9A%84%E7%9F%A5%E8%AF%86%E7%82%B9"><span class="toc-number">1.8.1.</span> <span class="toc-text">学习Thymeleaf必知的知识点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Springboot"><span class="toc-number">1.8.2.</span> <span class="toc-text">Springboot</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#MVC%E4%BB%8B%E7%BB%8D"><span class="toc-number">1.8.3.</span> <span class="toc-text">MVC介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-1%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB"><span class="toc-number">1.8.4.</span> <span class="toc-text">8.1动静分离</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-2-%E5%88%9B%E5%BB%BA%E7%AC%AC%E4%B8%80%E4%B8%AAthymeleaf%E9%A1%B5%E9%9D%A2"><span class="toc-number">1.8.5.</span> <span class="toc-text">8.2 创建第一个thymeleaf页面</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E6%A0%87%E7%AD%BE"><span class="toc-number">1.8.5.1.</span> <span class="toc-text">常用标签</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%93%BE%E6%8E%A5%E8%A1%A8%E8%BE%BE%E5%BC%8F-%E2%80%A6"><span class="toc-number">1.8.5.2.</span> <span class="toc-text">链接表达式: @{…}</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E8%A1%A8%E8%BE%BE%E5%BC%8F-%E2%80%A6"><span class="toc-number">1.8.5.3.</span> <span class="toc-text">变量表达式: ${…}</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%80%89%E6%8B%A9%E5%8F%98%E9%87%8F%E8%A1%A8%E8%BE%BE%E5%BC%8F-%E2%80%A6"><span class="toc-number">1.8.5.4.</span> <span class="toc-text">选择变量表达式: *{…}</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%B6%88%E6%81%AF%E8%A1%A8%E8%BE%BE-%E2%80%A6"><span class="toc-number">1.8.5.5.</span> <span class="toc-text">消息表达: #{…}</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">1.8.5.6.</span> <span class="toc-text">总结</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#9-Servlet%E4%BF%9D%E5%AD%98%E5%9F%9F"><span class="toc-number">1.9.</span> <span class="toc-text">9.Servlet保存域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-%E8%B7%AF%E5%BE%84%E9%97%AE%E9%A2%98"><span class="toc-number">1.10.</span> <span class="toc-text">10.路径问题</span></a></li></ol></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        servlet
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">云雪</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2023-09-12T14:06:36.000Z" itemprop="datePublished">2023-09-12</time>
        
      
    </div>


      
    <div class="article-category">
        <i class="fas fa-archive"></i>
        <a class="category-link" href="/categories/java/">java</a>
    </div>


      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link-link" href="/tags/servlet/" rel="tag">servlet</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h2 id="Servlet"><a href="#Servlet" class="headerlink" title="Servlet"></a>Servlet</h2><h3 id="1-Servlet获取参数"><a href="#1-Servlet获取参数" class="headerlink" title="1.Servlet获取参数"></a>1.Servlet获取参数</h3><h4 id="1-1编写页面"><a href="#1-1编写页面" class="headerlink" title="1.1编写页面"></a>1.1编写页面</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Title<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">&quot;add&quot;</span> <span class="attr">method</span>=<span class="string">&quot;post&quot;</span>&gt;</span></span><br><span class="line">  名称:<span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;fname&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">  价格:<span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;price&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">  库存:<span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;text&quot;</span> <span class="attr">name</span>=<span class="string">&quot;count&quot;</span>&gt;</span><span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;submit&quot;</span> <span class="attr">value</span>=<span class="string">&quot;添加&quot;</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="1-2创建servlet类"><a href="#1-2创建servlet类" class="headerlink" title="1.2创建servlet类"></a>1.2创建servlet类</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> com.mihayo.servlet;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> javax.servlet.ServletException;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServlet;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletRequest;</span><br><span class="line"><span class="keyword">import</span> javax.servlet.http.HttpServletResponse;</span><br><span class="line"><span class="keyword">import</span> java.io.IOException;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * ClassName: AddServlet</span></span><br><span class="line"><span class="comment"> * Package: com.mihayo.servlet</span></span><br><span class="line"><span class="comment"> * Description:</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Author</span>: 余泽云</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Create</span>: 2023/9/12 - 22:17</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@Version</span>: v1.0</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">AddServlet</span> <span class="keyword">extends</span> <span class="title class_">HttpServlet</span> &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">doPost</span><span class="params">(HttpServletRequest request, HttpServletResponse response)</span> <span class="keyword">throws</span> ServletException, IOException &#123;</span><br><span class="line">        <span class="type">String</span> <span class="variable">fname</span> <span class="operator">=</span> request.getParameter(<span class="string">&quot;fname&quot;</span>);</span><br><span class="line">        <span class="type">String</span> <span class="variable">priceStr</span> <span class="operator">=</span> request.getParameter(<span class="string">&quot;price&quot;</span>);</span><br><span class="line"><span class="comment">//      接收的类型是字符串，需要进行强制类型转换</span></span><br><span class="line">        <span class="type">Integer</span> <span class="variable">price</span> <span class="operator">=</span> Integer.getInteger(priceStr);</span><br><span class="line">        <span class="type">String</span> <span class="variable">countStr</span> <span class="operator">=</span> request.getParameter(<span class="string">&quot;count&quot;</span>);</span><br><span class="line">        <span class="type">Integer</span> <span class="variable">count</span> <span class="operator">=</span> Integer.getInteger(countStr);</span><br><span class="line"></span><br><span class="line">        System.out.println(fname);</span><br><span class="line">        System.out.println(price);</span><br><span class="line">        System.out.println(count);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="1-3配置web-xml"><a href="#1-3配置web-xml" class="headerlink" title="1.3配置web.xml"></a>1.3配置web.xml</h4><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">web-app</span> <span class="keyword">PUBLIC</span></span></span><br><span class="line"><span class="meta">		<span class="string">&quot;-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN&quot;</span></span></span><br><span class="line"><span class="meta">		<span class="string">&quot;http://java.sun.com/dtd/web-app_2_3.dtd&quot;</span> &gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">web-app</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>AddServlet<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.mihayo.servlet.AddServlet<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>AddServlet<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">		<span class="tag">&lt;<span class="name">url-pattern</span>&gt;</span>/add<span class="tag">&lt;/<span class="name">url-pattern</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;/<span class="name">servlet-mapping</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--</span></span><br><span class="line"><span class="comment">1.用户发请求，action=add</span></span><br><span class="line"><span class="comment">2.项目中，web.xml中找到url-pattern = /add</span></span><br><span class="line"><span class="comment">3.找第11行的servlet-name = AddServlet</span></span><br><span class="line"><span class="comment">4.找和servlet-mapping中servlet-name一致的servlet,找到第7行</span></span><br><span class="line"><span class="comment">5.找第8行的servlet-class -&gt; com.mihayo.servlet.AddServlet</span></span><br><span class="line"><span class="comment">6.用户发送的是post请求（method=post）,因此tomcat会执行AddServlet中的doPost方法</span></span><br><span class="line"><span class="comment">--&gt;</span> </span><br><span class="line"><span class="tag">&lt;/<span class="name">web-app</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h4 id="1-4启动tomcat"><a href="#1-4启动tomcat" class="headerlink" title="1.4启动tomcat"></a>1.4启动tomcat</h4><p>在页面填写信息提交，查看idea后台输出结果</p>
<h3 id="2-处理请求参数中文乱码问题"><a href="#2-处理请求参数中文乱码问题" class="headerlink" title="2.处理请求参数中文乱码问题"></a>2.处理请求参数中文乱码问题</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//post方式下，设置编码，防止中文乱码</span></span><br><span class="line"><span class="comment">//需要注意的是，设置编码这一句代码必须在所有的获取参数动作之前</span></span><br><span class="line">request.setCharacterEncoding(<span class="string">&quot;UTF-8&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">//get方式下，目前不需要设置编码（基于tomcat8）</span></span><br><span class="line"><span class="comment">//如果是get请求发送的中文数据，转码稍微有点麻烦（tomcat8之前）</span></span><br><span class="line"><span class="type">String</span> <span class="variable">fname</span> <span class="operator">=</span> request.getParameter(<span class="string">&quot;fname&quot;</span>);</span><br><span class="line"><span class="comment">//1.将字符串打散成字节数组</span></span><br><span class="line"><span class="type">byte</span>[] bytes = fname.getBytes(<span class="string">&quot;ISO-8859-1&quot;</span>);</span><br><span class="line"><span class="comment">//2.将字节数组按照设定的编码重新组装成字符串</span></span><br><span class="line">fname = <span class="keyword">new</span> <span class="title class_">String</span>(bytes,<span class="string">&quot;UTF-8&quot;</span>);</span><br></pre></td></tr></table></figure>

<h3 id="3-Servlet的继承关系"><a href="#3-Servlet的继承关系" class="headerlink" title="3.Servlet的继承关系"></a>3.Servlet的继承关系</h3><blockquote>
<p>继承关系：</p>
<ol>
<li>javax.servlet.Servlet接口</li>
<li>javax.servlet.GenericServlet抽象类</li>
<li>javax.servlet.http.HttpServlet抽象子类</li>
</ol>
<p>相关方法：</p>
<p>javax.servlet.Servlet接口：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//初始化方法</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">init</span><span class="params">(ServletConfig var1)</span> <span class="keyword">throws</span> ServletException;</span><br><span class="line"><span class="comment">//服务方法</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">service</span><span class="params">(ServletRequest var1, ServletResponse var2)</span> <span class="keyword">throws</span> ServletException, IOException;</span><br><span class="line"><span class="comment">//销毁方法</span></span><br><span class="line"><span class="keyword">void</span> <span class="title function_">destroy</span><span class="params">()</span>;</span><br></pre></td></tr></table></figure>

<p>javax.servlet.GennericServlet抽象类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//依旧是抽象方法</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">void</span> <span class="title function_">service</span><span class="params">(ServletRequest var1, ServletResponse var2)</span> <span class="keyword">throws</span> ServletException, IOException;</span><br></pre></td></tr></table></figure>

<p>javax.servlet.http.HttpServlet抽象子类：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title function_">service</span><span class="params">(HttpServletRequest req, HttpServletResponse resp)</span> <span class="keyword">throws</span> ServletException, IOException &#123;</span><br><span class="line">    <span class="comment">//获取请求参数的类型</span></span><br><span class="line">    <span class="type">String</span> <span class="variable">method</span> <span class="operator">=</span> req.getMethod();</span><br><span class="line">       <span class="type">long</span> lastModified;</span><br><span class="line">    <span class="comment">//进行各种if判断，根据请求方式不同，调用不同发doxxx方法</span></span><br><span class="line">       <span class="keyword">if</span> (method.equals(<span class="string">&quot;GET&quot;</span>)) &#123;</span><br><span class="line">           lastModified = <span class="built_in">this</span>.getLastModified(req);</span><br><span class="line">           <span class="keyword">if</span> (lastModified == -<span class="number">1L</span>) &#123;</span><br><span class="line">               <span class="built_in">this</span>.doGet(req, resp);</span><br><span class="line">           &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">               <span class="type">long</span> <span class="variable">ifModifiedSince</span> <span class="operator">=</span> req.getDateHeader(<span class="string">&quot;If-Modified-Since&quot;</span>);</span><br><span class="line">               <span class="keyword">if</span> (ifModifiedSince &lt; lastModified / <span class="number">1000L</span> * <span class="number">1000L</span>) &#123;</span><br><span class="line">                   <span class="built_in">this</span>.maybeSetLastModified(resp, lastModified);</span><br><span class="line">                   <span class="built_in">this</span>.doGet(req, resp);</span><br><span class="line">               &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                   resp.setStatus(<span class="number">304</span>);</span><br><span class="line">               &#125;</span><br><span class="line">           &#125;</span><br><span class="line">       &#125; <span class="keyword">else</span> <span class="keyword">if</span> (method.equals(<span class="string">&quot;HEAD&quot;</span>)) &#123;</span><br><span class="line">           lastModified = <span class="built_in">this</span>.getLastModified(req);</span><br><span class="line">           <span class="built_in">this</span>.maybeSetLastModified(resp, lastModified);</span><br><span class="line">           <span class="built_in">this</span>.doHead(req, resp);</span><br><span class="line">       &#125; <span class="keyword">else</span> <span class="keyword">if</span> (method.equals(<span class="string">&quot;POST&quot;</span>)) &#123;</span><br><span class="line">           <span class="comment">//常用</span></span><br><span class="line">           <span class="built_in">this</span>.doPost(req, resp);</span><br><span class="line">       &#125; <span class="keyword">else</span> <span class="keyword">if</span> (method.equals(<span class="string">&quot;PUT&quot;</span>)) &#123;</span><br><span class="line">           <span class="built_in">this</span>.doPut(req, resp);</span><br><span class="line">       &#125; <span class="keyword">else</span> <span class="keyword">if</span> (method.equals(<span class="string">&quot;DELETE&quot;</span>)) &#123;</span><br><span class="line">           <span class="built_in">this</span>.doDelete(req, resp);</span><br><span class="line">       &#125; <span class="keyword">else</span> <span class="keyword">if</span> (method.equals(<span class="string">&quot;OPTIONS&quot;</span>)) &#123;</span><br><span class="line">           <span class="built_in">this</span>.doOptions(req, resp);</span><br><span class="line">       &#125; <span class="keyword">else</span> <span class="keyword">if</span> (method.equals(<span class="string">&quot;TRACE&quot;</span>)) &#123;</span><br><span class="line">           <span class="built_in">this</span>.doTrace(req, resp);</span><br><span class="line">       &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">           <span class="type">String</span> <span class="variable">errMsg</span> <span class="operator">=</span> lStrings.getString(<span class="string">&quot;http.method_not_implemented&quot;</span>);</span><br><span class="line">           Object[] errArgs = <span class="keyword">new</span> <span class="title class_">Object</span>[]&#123;method&#125;;</span><br><span class="line">           errMsg = MessageFormat.format(errMsg, errArgs);</span><br><span class="line">           resp.sendError(<span class="number">501</span>, errMsg);</span><br><span class="line">       &#125;</span><br><span class="line"></span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>

<p>小结：</p>
<ul>
<li>继承关系：<strong>HttpServlet–&gt;GenericServlet-&gt;Servlet</strong></li>
<li>Servlet中的核心方法：<code>init()</code>，<code>service()</code>，<code>destroy()</code></li>
<li>服务方法：<ul>
<li>当有请求方法过来，service方法会自动响应(其实是tomcat容器调用的)</li>
<li>在HttpServlet中我们会去分析请求的方式：到底是<code>get</code>、<code>post</code>、<code>head</code>还是<code>delete</code>等等，然后再决定去调用的是哪个do开头的方法</li>
<li>在HttpServlet中这些do方法默认都是405的实现风格–要我们子类去实现对应的方法，或者会暴404错误</li>
<li>因此，我们在新建Servlet时，我们才会去考虑请求方法，从而决定重写哪个do方法</li>
</ul>
</li>
</ul>
</blockquote>
<h3 id="4-Servlet生命周期"><a href="#4-Servlet生命周期" class="headerlink" title="4.Servlet生命周期"></a>4.Servlet生命周期</h3><blockquote>
<ul>
<li><p><strong>生命周期</strong>：从出生到死亡的过程就是生命周期，对应Servlet中的<code>init()</code>方法、<code>servlet()</code>方法、<code>destory()</code>方法</p>
</li>
<li><p>默认情况下：</p>
<ul>
<li>第一次接收请求时，这个Servlet会进行实例化(调用构造方法)、初始化(调用init())、然后服务(调用servlet())</li>
<li>从第二次请求开始，每一次都是服务，当容器关闭时，其中的所有的servlet实例会被销毁，调用销毁方法(destory())</li>
</ul>
</li>
<li><p>通过案例分析：</p>
<ul>
<li>Servlet实例，Tomact只会创建一个，所有的请求都是这个实例去响应</li>
<li>默认情况下，第一次请求时，Tomcat才会去实例化，初始化，然后再服务，这样的好处是什么？提高系统的启动速度，这样的缺点是什么？第一次请求时，会比较耗时</li>
<li>因此得出结论：如果需要提高系统的启动速度，当前默认情况是这样，如果需要提高启动速度，则可以使用默认情况，如果需要提高响应速度，则应该设置Servlet初始化时机</li>
</ul>
</li>
<li><p>Servlet的初始化时机：</p>
<ul>
<li><p>默认是第一次接收，实例化、初始化</p>
</li>
<li><p>我们可以通过<load-on-startup>来设置servlet启动的先后顺序，数字越小，启动越靠前，最小值为0</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">servlet</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">servlet-name</span>&gt;</span>AddServlet<span class="tag">&lt;/<span class="name">servlet-name</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">servlet-class</span>&gt;</span>com.mihayo.servlet.AddServlet<span class="tag">&lt;/<span class="name">servlet-class</span>&gt;</span></span><br><span class="line">	<span class="tag">&lt;<span class="name">load-on-startup</span>&gt;</span>1<span class="tag">&lt;/<span class="name">load-on-startup</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">servlet</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>Servlet在容器中是单例的，线程不安全的</p>
<ul>
<li>单例：所有的请求都是同一个实例去响应</li>
<li>线程不安全：一个线程需要根据这个实例中的某个成员变量值去做逻辑判断，但是在中间目某个时机，另一个线程改变了成员变量的值，从而导致第一个线程的执行路径发生的改变</li>
<li>我们已经知道了servlet是线程不安全的，给我们的启发是：尽量不要在servlet中定义成员变量，如果不得不定义定义成员变量，那么就不要去：<ul>
<li>不要去修改成员变量的值</li>
<li>不要根据成员变量进行逻辑判断</li>
</ul>
</li>
</ul>
</li>
</ul>
</blockquote>
<h3 id="5-Http协议"><a href="#5-Http协议" class="headerlink" title="5.Http协议"></a>5.Http协议</h3><h4 id="5-1Http协议简介"><a href="#5-1Http协议简介" class="headerlink" title="5.1Http协议简介"></a>5.1Http协议简介</h4><blockquote>
<ul>
<li>HTTP 协议是 Hyper Text Transfer Protocol（超文本传输协议）的缩写，是用于从万维网（ WWW:World Wide Web ）服务器传输超文本到本地浏览器的传送协议。</li>
<li>HTTP 是一个基于 TCP&#x2F;IP 通信协议来传递数据（HTML 文件、图片文件、查询结果等）。</li>
<li>HTTPS 协议是 HyperText Transfer Protocol Secure（超文本传输安全协议）的缩写，是一种通过计算机网络进行安全通信的传输协议。</li>
<li>HTTPS 经由 HTTP 进行通信，但利用 SSL&#x2F;TLS 来加密数据包，HTTPS 开发的主要目的，是提供对网站服务器的身份认证，保护交换资料的隐私与完整性。</li>
<li>HTTP 的 URL 是由 <strong>http:&#x2F;&#x2F;</strong> 起始与默认使用端口 <strong>80</strong>，而 HTTPS 的 URL 则是由 <strong>https:&#x2F;&#x2F;</strong> 起始与默认使用端口<strong>443</strong></li>
</ul>
</blockquote>
<h4 id="5-2HTTP-工作原理"><a href="#5-2HTTP-工作原理" class="headerlink" title="5.2HTTP 工作原理"></a>5.2HTTP 工作原理</h4><blockquote>
<p>HTTP 协议工作于客户端-服务端架构上。</p>
<p>浏览器作为 HTTP 客户端通过 URL 向 HTTP 服务端即 WEB 服务器发送所有请求。</p>
<p>Web 服务器有：Apache 服务器，IIS 服务器（Internet Information Services）等。</p>
<p>Web 服务器根据接收到的请求后，向客户端发送响应信息。</p>
<p>HTTP 默认端口号为 80，但是你也可以改为 8080 或者其他端口。</p>
</blockquote>
<p>以下图表展示了 HTTP 协议通信流程：</p>
<p><img src="/servlet.assets/cgiarch.gif" alt="cgiarch"></p>
<h4 id="5-3请求方法"><a href="#5-3请求方法" class="headerlink" title="5.3请求方法"></a>5.3请求方法</h4><p>根据 HTTP 标准，HTTP 请求可以使用多种请求方法。</p>
<p>HTTP1.0 定义了三种请求方法： <code>GET</code>, <code>POST</code> 和 <code>HEAD</code> 方法。</p>
<p>HTTP1.1 新增了六种请求方法：<code>OPTIONS</code>、<code>PUT</code>、<code>PATCH</code>、<code>DELETE</code>、<code>TRACE</code> 和 <code>CONNECT</code> 方法。</p>
<table>
<thead>
<tr>
<th align="left">序号</th>
<th align="left">方法</th>
<th align="left">描述</th>
</tr>
</thead>
<tbody><tr>
<td align="left">1</td>
<td align="left">GET</td>
<td align="left">请求指定的页面信息，并返回实体主体。</td>
</tr>
<tr>
<td align="left">2</td>
<td align="left">HEAD</td>
<td align="left">类似于 GET 请求，只不过返回的响应中没有具体的内容，用于获取报头</td>
</tr>
<tr>
<td align="left">3</td>
<td align="left">POST</td>
<td align="left">向指定资源提交数据进行处理请求（例如提交表单或者上传文件）。数据被包含在请求体中。POST 请求可能会导致新的资源的建立和&#x2F;或已有资源的修改。</td>
</tr>
<tr>
<td align="left">4</td>
<td align="left">PUT</td>
<td align="left">从客户端向服务器传送的数据取代指定的文档的内容。</td>
</tr>
<tr>
<td align="left">5</td>
<td align="left">DELETE</td>
<td align="left">请求服务器删除指定的页面。</td>
</tr>
<tr>
<td align="left">6</td>
<td align="left">CONNECT</td>
<td align="left">HTTP&#x2F;1.1 协议中预留给能够将连接改为管道方式的代理服务器。</td>
</tr>
<tr>
<td align="left">7</td>
<td align="left">OPTIONS</td>
<td align="left">允许客户端查看服务器的性能。</td>
</tr>
<tr>
<td align="left">8</td>
<td align="left">TRACE</td>
<td align="left">回显服务器收到的请求，主要用于测试或诊断。</td>
</tr>
<tr>
<td align="left">9</td>
<td align="left">PATCH</td>
<td align="left">是对 PUT 方法的补充，用来对已知资源进行局部更新 。</td>
</tr>
</tbody></table>
<h4 id="5-4请求消息头"><a href="#5-4请求消息头" class="headerlink" title="5.4请求消息头"></a>5.4请求消息头</h4><ul>
<li><p>作用：通过具体的参数对本次请求进行详细的说明</p>
</li>
<li><p>格式：键值对，键和值之间使用冒号隔开</p>
</li>
<li><p>相对比较重要的请求消息头：</p>
<table>
<thead>
<tr>
<th>名称</th>
<th>功能</th>
</tr>
</thead>
<tbody><tr>
<td><code>Host</code></td>
<td>服务器的主机地址</td>
</tr>
<tr>
<td><code>Accept</code></td>
<td>声明当前请求能够接收的【媒体类型】</td>
</tr>
<tr>
<td><code>Referer</code></td>
<td>当前请求来源页面的地址</td>
</tr>
<tr>
<td><code>Conten-Length</code></td>
<td>请求内容的长度</td>
</tr>
<tr>
<td><code>Content-Type</code></td>
<td>请求体的内容类型，这一项的具体是媒体类型中的某一种</td>
</tr>
<tr>
<td><code>Cookie</code></td>
<td>浏览器访问服务器是携带的Cookie数据</td>
</tr>
</tbody></table>
</li>
</ul>
<h4 id="5-5请求体"><a href="#5-5请求体" class="headerlink" title="5.5请求体"></a>5.5请求体</h4><ul>
<li>请求体包含三个部分：<ul>
<li>请求行 </li>
<li>请求消息头 </li>
<li>请求主体</li>
</ul>
</li>
<li>请求行包含是三个消息：<ul>
<li>请求的方式</li>
<li>请求的url</li>
<li>请求的协议（一般是HTTP1.1）</li>
</ul>
</li>
<li>请求消息头包含了很多客服端需要告诉服务器的信息</li>
<li>请求体：<ul>
<li><strong>get</strong>方式：没有请求体，但是有一个<code>queryString</code></li>
<li><strong>post</strong>方式：有请求体，<code>form</code> <code>data</code></li>
<li><strong>json</strong>方式：有请求体，<code>request</code> <code>payload</code></li>
</ul>
</li>
</ul>
<h4 id="5-6响应"><a href="#5-6响应" class="headerlink" title="5.6响应"></a>5.6响应</h4><ul>
<li>响应包含三个部分：<ul>
<li>响应行</li>
<li>响应头</li>
<li>响应体</li>
</ul>
</li>
<li>响应行包含三个信息<ul>
<li>协议</li>
<li>响应状态码</li>
<li>响应状态</li>
</ul>
</li>
<li>响应头：<ul>
<li>包含了服务器的信息；</li>
<li>服务器发送给浏览器的信息（内容的媒体类型、编码、内容长度等）</li>
</ul>
</li>
<li>响应体：响应的实际内容（比如请求add.html页面时，响应的内容就是）</li>
</ul>
<h4 id="5-7状态码"><a href="#5-7状态码" class="headerlink" title="5.7状态码"></a>5.7状态码</h4><p>当浏览者访问一个网页时，浏览者的浏览器会向网页所在服务器发出请求。当浏览器接收并显示网页前，此网页所在的服务器会返回一个包含 HTTP 状态码的信息头（server header）用以响应浏览器的请求。</p>
<p>HTTP 状态码的英文为 <strong>HTTP Status Code</strong>。。</p>
<p>下面是常见的 HTTP 状态码：</p>
<ul>
<li>200 - 请求成功</li>
<li>301 - 资源（网页等）被永久转移到其它URL</li>
<li>404 - 请求的资源（网页等）不存在</li>
<li>500 - 内部服务器错误</li>
</ul>
<h3 id="6-会话"><a href="#6-会话" class="headerlink" title="6.会话"></a>6.会话</h3><h4 id="6-1HTTP是无状态的"><a href="#6-1HTTP是无状态的" class="headerlink" title="6.1HTTP是无状态的"></a>6.1HTTP是无状态的</h4><ul>
<li>HTTP无状态：服务器无法判断这两次请求时同一个客服端发过来的，还是不同的客服端发送过来的</li>
<li>无状态带来的现实问题：第一次请求时添加商品到购物车，第二次请求时结账；如果这两次请求服务器无法区分，那么就会导致混乱</li>
<li>通过会话跟踪技术来解决无状态问题</li>
</ul>
<h4 id="6-2会话跟踪技术"><a href="#6-2会话跟踪技术" class="headerlink" title="6.2会话跟踪技术"></a>6.2会话跟踪技术</h4><ul>
<li>客户端第一次发请求给服务器，服务器获取session，获取不到，则创建新的，然后响应给客服端</li>
<li>下次客户端给服务器发请求时，会把sessionID带给服务器，那么服务器就获取到了，那么服务器就判断这次请求和上次请求是否为同一个</li>
<li>常用API：<ul>
<li><code>request.getSession()</code> -&gt;获取当前的会话，没有创建一个新的会话</li>
<li><code>request.getSession(true)</code> -&gt;效果和不带参数相同</li>
<li><code>request.getSession(false)</code> -&gt;获取当前会话，没有则返回null，不会创建新的</li>
<li><code>session.getId()</code> -&gt;获取sessionID</li>
<li><code>session.isNew()</code> -&gt;判断当前session是否是新的</li>
<li><code>session.getMaxInactiveInterval()</code> -&gt;session的非激活间隔时长，默认1800秒</li>
<li><code>session.invalidate()</code> -&gt;强制性让会话立即失效</li>
</ul>
</li>
</ul>
<h4 id="6-3session保存作用域"><a href="#6-3session保存作用域" class="headerlink" title="6.3session保存作用域"></a>6.3session保存作用域</h4><ul>
<li>session保存作用域是和具体某一个session对应的</li>
<li>常用API：<ul>
<li><code>void session.setAttribute(k,v)</code></li>
<li><code>Object session.getAttributr(k)</code></li>
<li><code>void removeAttribute(k)</code></li>
</ul>
</li>
</ul>
<h3 id="7-服务器内部转发以及客服端重定向"><a href="#7-服务器内部转发以及客服端重定向" class="headerlink" title="7.服务器内部转发以及客服端重定向"></a>7.服务器内部转发以及客服端重定向</h3><ul>
<li>服务器内部转发：<code>request.getRequestDispatcher(&quot;...&quot;).forward(request,response)</code><ul>
<li>一次请求响应的过程，对于客户端而言，内部经过了多少次转发，客户端是不知道的</li>
<li>地址栏没有变化</li>
</ul>
</li>
<li>客户端重定向：<code>response.sendRedirect(&quot;...&quot;)</code><ul>
<li>两次请求响应的过程，客户端肯定知道请求URL有变化</li>
<li>地址栏有变化</li>
</ul>
</li>
</ul>
<h3 id="8-Thymeleaf–视图模版技术"><a href="#8-Thymeleaf–视图模版技术" class="headerlink" title="8.Thymeleaf–视图模版技术"></a>8.Thymeleaf–视图模版技术</h3><blockquote>
<ul>
<li><strong>模板引擎</strong>（这里特指用于Web开发的模板引擎）是为了使用户界面与业务数据（内容）分离而产生的，它可以生成特定格式的文档，用于网站的模板引擎就会生成一个标准的html文档。从字面上理解<code>模板引擎</code>，最重要的就是模板二字，这个意思就是做好一个模板后套入对应位置的数据，最终以html的格式展示出来，这就是模板引擎的作用。</li>
<li>对于模板引擎的理解，可以这样形象的做一个类比：<strong>开会！</strong> 相信你在上学初高中时候每次开会都要提前布置场地、拿小板凳、收拾场地。而你上了大学之后每次开会再也不去大操场了,每次开会都去学校的大会议室，桌子板凳音响主席台齐全，来个人即可，还可复用……。模板引擎的功能就类似我们的会议室开会一样开箱即用，将模板设计好之后直接填充数据即可而不需要重新设计整个页面。提高页面、代码的复用性。</li>
<li><strong>动静分离：</strong> Thymeleaf选用html作为模板页，这是任何一款其他模板引擎做不到的！Thymeleaf使用html通过一些特定标签语法代表其含义，但并未破坏html结构，即使无网络、不通过后端渲染也能在浏览器成功打开，大大方便界面的测试和修改。</li>
<li><strong>开箱即用：</strong> Thymeleaf提供标准和Spring标准两种方言，可以直接套用模板实现JSTL、 OGNL表达式效果，避免每天套模板、改JSTL、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。</li>
</ul>
</blockquote>
<h4 id="学习Thymeleaf必知的知识点"><a href="#学习Thymeleaf必知的知识点" class="headerlink" title="学习Thymeleaf必知的知识点"></a>学习Thymeleaf必知的知识点</h4><p>Thymeleaf模板的运行离不开web的环境，所以你需要对相关知识学习理解才能更好的有助于你对Thymeleaf的学习和认知。</p>
<h4 id="Springboot"><a href="#Springboot" class="headerlink" title="Springboot"></a>Springboot</h4><p>相信你对Springboot都很熟悉，我们使用Thymeleaf大多情况都是基于Springboot平台的，并且Thymeleaf的发展推广也离不开Springboot官方得支持，且本文的实战部分也是基于Springboot平台。</p>
<p>而Spring Boot是由Pivotal团队提供的全新框架，其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置，从而使开发人员不再需要定义样板化的配置。通过这种方式，Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。</p>
<p>简而言之，Springboot是当前web开发主流，且其简化了Spring的配置让开发者能够更容易上手Web项目的开发。且Thymeleaf能够快速整合入Springboot，使用方便快捷。</p>
<h4 id="MVC介绍"><a href="#MVC介绍" class="headerlink" title="MVC介绍"></a>MVC介绍</h4><p>我们使用的Thymeleaf模板引擎在整个web项目中起到的作用为视图展示(view)，谈到视图就不得不提起模型(model)以及控制器(view),其三者在web项目中分工和职责不同，但又相互有联系。三者组成当今web项目较为流行的MVC架构。</p>
<p>MVC全名是Model View Controller，是模型(model)－视图(view)－控制器(controller)的缩写，其中：</p>
<ul>
<li>Model（模型）表示应用程序核心（用来存储数据供视图层渲染）。</li>
<li>View（视图）显示数据，而本篇使用的就是Thymeleaf作为视图。</li>
<li>Controller（控制器）处理输入请求，将模型和视图分离。<br><img src="/servlet.assets/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjkzMTcx,size_1,color_FFFFFF,t_70-169468258611917.png" alt="在这里插入图片描述"></li>
</ul>
<p>使用MVC设计模式程序有很多优点，比如降低程序耦合、增加代码的复用性、降低开发程序和接口的成本，并且通过这样分层结构在部署维护能够提供更大的便捷性。</p>
<p>在Java web体系最流行的MVC框架无疑就是Springmvc框架了，在项目中经常配合模板引擎使用或者提供Restful接口。在下面案例Thymeleaf同样使用Springmvc作为MVC框架进行控制。</p>
<h4 id="8-1动静分离"><a href="#8-1动静分离" class="headerlink" title="8.1动静分离"></a>8.1动静分离</h4><p>你可能还是不明白什么才是真正的动静分离，其实这个主要是由于Thymeleaf模板基于html，后缀也是<code>.html</code>，所以这样就会产生一些有趣的灵魂。</p>
<p>对于传统jsp或者其他模板来说，没有一个模板引擎的后缀为<code>.html</code>，就拿jsp来说jsp的后缀为<code>.jsp</code>,它的本质就是将一个html文件修改后缀为<code>.jsp</code>，然后在这个文件中增加自己的语法、标签然后执行时候通过后台处理这个文件最终返回一个html页面。</p>
<p>浏览器无法直接识别<code>.jsp</code>文件，需要借助网络(服务端)才能进行访问；而Thymeleaf用html做模板可以直接在浏览器中打开。开发者充分考虑html页面特性，将Thymeleaf的语法通过html的标签属性来定义完成，这些标签属性不会影响html页面的完整性和显示。如果通过后台服务端访问页面服务端会寻找这些标签将服务端对应的数据替换到相应位置实现动态页面！大体区别可以参照下图：<br><img src="/servlet.assets/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjkzMTcx,size_1,color_FFFFFF,t_70-16946539928691.png" alt="在这里插入图片描述"><br>上图的意思就是如果直接打开这个html那么浏览器会对<code>th</code>等标签忽视而显示原始的内容。如果通过服务端访问那么服务端将先寻找<code>th</code>标签将服务端储存的数据替换到对应位置。</p>
<h4 id="8-2-创建第一个thymeleaf页面"><a href="#8-2-创建第一个thymeleaf页面" class="headerlink" title="8.2 创建第一个thymeleaf页面"></a>8.2 创建第一个thymeleaf页面</h4><ul>
<li><p>创建程序，向pox.xml添加依赖</p>
<ul>
<li><pre><code class="xml">        &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
              &lt;version&gt;2.3.7.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- thymeleaf--&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-thymeleaf&lt;/artifactId&gt;
              &lt;version&gt;2.3.7.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">  - 项目结构</span><br><span class="line"></span><br><span class="line">    - **pom.xml**：是项目中的Maven依赖，因为Springboot使用Maven来管理外部jar包依赖，我们只需填写依赖名称配置即可引入该依赖，在本系统中引入Spring web模块(Springmvc)和Thymeleaf的依赖.我们不需要自己去招相关jar包。</span><br><span class="line">    - **application.properties**: 编写Springboot与各框架整合的一些配置内容。</span><br><span class="line">    - **controller**：用来编写控制器，主要负责处理请求以及和视图(Thymeleaf)绑定。</span><br><span class="line">    - **static**：用于存放静态资源，例如html、JavaScript、css以及图片等。</span><br><span class="line">    - **templates**：用来存放模板引擎Thymeleaf(本质依然是.html文件)</span><br><span class="line"></span><br><span class="line">- 编写controller层</span><br><span class="line"></span><br><span class="line">  - ```java</span><br><span class="line">    package com.Thymeleaf.controller;</span><br><span class="line">    </span><br><span class="line">    import org.springframework.stereotype.Controller;</span><br><span class="line">    import org.springframework.ui.Model;</span><br><span class="line">    import org.springframework.web.bind.annotation.GetMapping;</span><br><span class="line">    </span><br><span class="line">    @Controller</span><br><span class="line">    public class urlController &#123;</span><br><span class="line">        @GetMapping(&quot;index&quot;)//页面的url地址</span><br><span class="line">        public String getindex(Model model)//对应函数</span><br><span class="line">        &#123;</span><br><span class="line">            model.addAttribute(&quot;name&quot;,&quot;bigsai&quot;);</span><br><span class="line">            return &quot;index&quot;;//与templates中index.html对应</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

- **@controller** 注解的意思就是声明这个java文件为一个controller控制器。
- **@GetMapping(&quot;index&quot;)** 其中@GetMapping的意思是请求的方式为get方式(即可通过浏览器直接请求)，而里面的index表示这个页面(接口)的url地址(路径)。即在浏览器对项目网页访问的地址。
- **getindex()** 是@GetMapping(&quot;index&quot;)注解对应的函数，其类型为String类型返回一个字符串，参数Model类型即用来储存数据供我们Thymeleaf页面使用。
- **model.addAttribute(&quot;name&quot;,&quot;bigsai&quot;)** 就是Model存入数据的书写方式，Model是一个特殊的类，相当于维护一个Map一样，而Model中的数据通过controller层的关联绑定在view层(即Thymeleaf中)可以直接使用。
- **return &quot;hello&quot;**：这个index就是在templates目录下对应模板(本次为Thymeleaf模板)的名称，即应该对应hello.html这个Thymeleaf文件(与页面关联默认规则为：templates目录下`返回字符串.html`)。
</code></pre>
</li>
</ul>
</li>
<li><p>编写Thyemleaf页面</p>
<ul>
<li><pre><code class="html">&lt;!DOCTYPE html&gt;
&lt;html  xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;title&gt;title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
hello 第一个Thymeleaf程序
&lt;div th:text=&quot;$&#123;name&#125;&quot;&gt;name是bigsai(我是离线数据)&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">    - 在这个html文件中的`&lt;html&gt;`标签修改为`&lt;html xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;`这样在Thymeleaf中就可以使用Thymeleaf的语法和规范啦。</span><br><span class="line"></span><br><span class="line">- 启动程序</span><br><span class="line"></span><br><span class="line">  - 这样写好之后咱们执行这个Springboot程序。通过网络访问`http://localhost:8080/index`</span><br><span class="line">  - </span><br><span class="line">    ![在这里插入图片描述](servlet.assets/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjkzMTcx,size_1,color_FFFFFF,t_70-16946559278406.png)</span><br><span class="line">    - 上图**左侧为直接打开的静态页面**，而**右侧是通过网络访问服务端返回的动态界面**，可以看的到，界面的内容和我们预期一致，左右两侧也正是静动态页面的两个代表。通过以上的步骤，你就完成了第一个Thymeleaf程序并且能够成功运行。入门便悄然开始！</span><br><span class="line"></span><br><span class="line">#### 8.3Thymeleaf语法详解</span><br><span class="line"></span><br><span class="line">上面虽然完成了第一个Thymeleaf程序，但是那样远远满足不了我们在项目中使用Thymeleaf，所以我们要对Thymeleaf的语法规则进行更详细的学习。</span><br><span class="line"></span><br><span class="line">##### 配置</span><br><span class="line"></span><br><span class="line">虽然Springboot官方对Thymeleaf做了很多默认配置，但咱们引入Thymeleaf的jar包依赖后很可能根据自己特定需求进行更细化的配置，例如页面缓存、字体格式设置等等。</span><br><span class="line"></span><br><span class="line">**Springboot官方提供的配置内容有以下：**</span><br><span class="line"></span><br><span class="line">```properties</span><br><span class="line"># THYMELEAF (ThymeleafAutoConfiguration)</span><br><span class="line">spring.thymeleaf.cache=true # 是否启用模板缓存.</span><br><span class="line">spring.thymeleaf.check-template=true # 在呈现模板之前是否检查它是否存在.</span><br><span class="line">spring.thymeleaf.check-template-location=true # 是否检查模板位置是否存在.</span><br><span class="line">spring.thymeleaf.enabled=true # 是否为Web框架启用Thymeleaf视图解析.</span><br><span class="line">spring.thymeleaf.enable-spring-el-compiler=false # 在 SpringEL 表达式中启用 SpringEL 编译器.</span><br><span class="line">spring.thymeleaf.encoding=UTF-8 # 模板文件编码.</span><br><span class="line">spring.thymeleaf.excluded-view-names= # 以逗号分隔的视图名称（允许模式）列表，这些视图名称应排除在解析之外.</span><br><span class="line">spring.thymeleaf.mode=HTML # 应用于模板的模板模式。另请参阅 Thymeleaf 的模板模式枚举.</span><br><span class="line">spring.thymeleaf.prefix=classpath:/templates/ # 在创建 UR 时为视图名称预置的前缀L.</span><br><span class="line">spring.thymeleaf.reactive.chunked-mode-view-names= # 以逗号分隔的视图名称（允许模式）列表，当设置了最大分块大小时，这些视图名称应是 CHUNKED 模式下唯一执行的视图名称.</span><br><span class="line">spring.thymeleaf.reactive.full-mode-view-names= # 以逗号分隔的视图名称（允许模式）列表，即使设置了最大块大小，这些视图名称也应在完整模式下执行.</span><br><span class="line">spring.thymeleaf.reactive.max-chunk-size=0 # 用于写入响应的数据缓冲区的最大大小（字节).</span><br><span class="line">spring.thymeleaf.reactive.media-types= # 视图技术所支持的媒体类型.</span><br><span class="line">spring.thymeleaf.servlet.content-type=text/html # 写入 HTTP 响应的 Content-Type 值s.</span><br><span class="line">spring.thymeleaf.suffix=.html # 构建 UR 时附加到视图名称的后缀L.</span><br><span class="line">spring.thymeleaf.template-resolver-order= # 模板解析器在链中的顺序.</span><br><span class="line">spring.thymeleaf.view-names= # 以逗号分隔的可解析视图名称（允许模式）列表.</span><br></pre></td></tr></table></figure>
</code></pre>
</li>
</ul>
</li>
</ul>
<p>上面的配置有些我们可能不常使用，因为Springboot官方做了默认配置大部分能够满足我们的使用需求，但如果你的项目有特殊需求也需要妥善使用这些配置。</p>
<p>比如<code>spring.thymeleaf.cache=false</code>是否允许页面缓存的配置，我们在开发时候要确保页面是最新的所以需要禁用缓存；而在上线运营时可能页面不常改动为了减少服务端压力以及提升客户端响应速度会允许页面缓存的使用。</p>
<p>再比如在开发虽然我们大部分使用UTF-8多一些，我们可以使用<code>spring.thymeleaf.encoding=UTF-8</code>来确定页面的编码，但如果你的项目是GBK编码就需要将它改成GBK。</p>
<p>另外Springboot默认模板引擎文件是放在templates目录下：<code>spring.thymeleaf.prefix=classpath:/templates/</code>,如果你有需求将模板引擎也可修改配置，将templates改为自己需要的目录。同理其他的配置如果需要自定义化也可参照上面配置进行修改。</p>
<h5 id="常用标签"><a href="#常用标签" class="headerlink" title="常用标签"></a>常用标签</h5><p>咱们上面知道Thymeleaf通过特殊的标签来寻找属于Thymeleaf的部分，并渲染该部分内容，而除了上面展示过的<code>th:text</code>之外还有很多常用标签，并且Thymeleaf也主要通过标签来识别替换对应位置内容，Thymeleaf标签有很多很多，功能也很丰富，这里列举一些比较常用的标签如下：</p>
<table>
<thead>
<tr>
<th>标签</th>
<th>作用</th>
<th>示例</th>
</tr>
</thead>
<tbody><tr>
<td>th:id</td>
<td>替换id</td>
<td><code>&lt;input th:id=&quot;$&#123;user.id&#125;&quot;/&gt;</code></td>
</tr>
<tr>
<td>th:text</td>
<td>文本替换</td>
<td><code>&lt;p text:=&quot;$&#123;user.name&#125;&quot;&gt;bigsai&lt;/p&gt;</code></td>
</tr>
<tr>
<td>th:utext</td>
<td>支持html的文本替换</td>
<td><code>&lt;p utext:=&quot;$&#123;htmlcontent&#125;&quot;&gt;content&lt;/p&gt;</code></td>
</tr>
<tr>
<td>th:object</td>
<td>替换对象</td>
<td><code>&lt;div th:object=&quot;$&#123;user&#125;&quot;&gt;&lt;/div&gt;</code></td>
</tr>
<tr>
<td>th:value</td>
<td>替换值</td>
<td><code>&lt;input th:value=&quot;$&#123;user.name&#125;&quot; &gt;</code></td>
</tr>
<tr>
<td>th:each</td>
<td>迭代</td>
<td><code>&lt;tr th:each=&quot;student:$&#123;user&#125;&quot; &gt;</code></td>
</tr>
<tr>
<td>th:href</td>
<td>替换超链接</td>
<td><code>&lt;a th:href=&quot;@&#123;index.html&#125;&quot;&gt;超链接&lt;/a&gt;</code></td>
</tr>
<tr>
<td>th:src</td>
<td>替换资源</td>
<td><code>&lt;script type=&quot;text/javascript&quot; th:src=&quot;@&#123;index.js&#125;&quot;&gt;&lt;/script&gt;</code></td>
</tr>
</tbody></table>
<h5 id="链接表达式-…"><a href="#链接表达式-…" class="headerlink" title="链接表达式: @{…}"></a>链接表达式: @{…}</h5><p>上面我们已经学习到Thymeleaf是一个基于html的模板引擎，但是我们还是需要加入特定标签来声明和使用Thymeleaf的语法。我们需要在Thymeleaf的头部加Thymeleaf标识：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">xmlns:th</span>=<span class="string">&quot;http://www.thymeleaf.org&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>在Thymeleaf 中，如果想引入链接比如link，href，src，需要使用<code>@&#123;资源地址&#125;</code>引入资源。<em>其中资源地址可以static目录下的静态资源，也可以是互联网中的绝对资源</em>。</p>
<p><strong>引入css</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">th:href</span>=<span class="string">&quot;@&#123;index.css&#125;&quot;</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>引入JavaScript：</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">script</span> <span class="attr">type</span>=<span class="string">&quot;text/javascript&quot;</span> <span class="attr">th:src</span>=<span class="string">&quot;@&#123;index.js&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><strong>超链接：</strong></p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">th:href</span>=<span class="string">&quot;@&#123;index.html&#125;&quot;</span>&gt;</span>超链接<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>这样启动程序访问页面，页面的内容就自动修改成标准html语法格式的内容：<br><img src="/servlet.assets/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQwNjkzMTcx,size_1,color_FFFFFF,t_70.png" alt="在这里插入图片描述"></p>
<h5 id="变量表达式-…"><a href="#变量表达式-…" class="headerlink" title="变量表达式: ${…}"></a>变量表达式: ${…}</h5><p>在Thymeleaf中可以通过${…}进行取值，这点和ONGL表达式语法一致。<br>例如咱们创建这么一个对象：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">user</span> &#123;</span><br><span class="line"> </span><br><span class="line">    <span class="keyword">private</span> String name;</span><br><span class="line">    <span class="keyword">private</span>  <span class="type">int</span> age;</span><br><span class="line">    <span class="keyword">private</span>  String detail;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">user</span><span class="params">(String name, <span class="type">int</span> age, String detail)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">        <span class="built_in">this</span>.detail = detail;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getName</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setName</span><span class="params">(String name)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.name = name;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getAge</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> age;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setAge</span><span class="params">(<span class="type">int</span> age)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.age = age;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">getDetail</span><span class="params">()</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> detail;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">setDetail</span><span class="params">(String detail)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.detail = detail;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>咱们为了测试先在Model中添一些数据：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@GetMapping(&quot;index&quot;)</span><span class="comment">//页面的url地址</span></span><br><span class="line"><span class="keyword">public</span> String <span class="title function_">getindex</span><span class="params">(Model model)</span><span class="comment">//对应函数</span></span><br><span class="line"> &#123;</span><br><span class="line">    user user1=<span class="keyword">new</span> <span class="title class_">user</span>(<span class="string">&quot;bigsai&quot;</span>,<span class="number">22</span>,<span class="string">&quot;一个幽默且热爱java的社会青年&quot;</span>);</span><br><span class="line">    List&lt;String&gt;userList=<span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">    userList.add(<span class="string">&quot;zhang san 66&quot;</span>);</span><br><span class="line">    userList.add(<span class="string">&quot;li si 66&quot;</span>);</span><br><span class="line">    userList.add(<span class="string">&quot;wang wu 66&quot;</span>);</span><br><span class="line">    Map&lt;String ,String&gt;map=<span class="keyword">new</span> <span class="title class_">HashMap</span>&lt;&gt;();</span><br><span class="line">    map.put(<span class="string">&quot;place&quot;</span>,<span class="string">&quot;博学谷&quot;</span>);</span><br><span class="line">    map.put(<span class="string">&quot;feeling&quot;</span>,<span class="string">&quot;very well&quot;</span>);</span><br><span class="line">    <span class="comment">//数据添加到model中</span></span><br><span class="line">    model.addAttribute(<span class="string">&quot;name&quot;</span>,<span class="string">&quot;bigsai&quot;</span>);<span class="comment">//普通字符串</span></span><br><span class="line">    model.addAttribute(<span class="string">&quot;user&quot;</span>,user1);<span class="comment">//储存javabean</span></span><br><span class="line">    model.addAttribute(<span class="string">&quot;userlist&quot;</span>,userList);<span class="comment">//储存List</span></span><br><span class="line">    model.addAttribute(<span class="string">&quot;map&quot;</span>,map);<span class="comment">//储存Map</span></span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;index&quot;</span>;<span class="comment">//与templates中index.html对应</span></span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>

<p><strong>取普通字符串：</strong><br>如果在controller中的Model直接存储某字符串，我们可以直接<code>$&#123;对象名&#125;</code>进行取值。完整代码如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>普通字符串<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">table</span> <span class="attr">border</span>=<span class="string">&quot;0&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span> <span class="attr">th:text</span>=<span class="string">&quot;&#x27;我的名字是：&#x27;+$&#123;name&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>运行结果为：<br><img src="/servlet.assets/2020071011334452.png" alt="在这里插入图片描述"><br><strong>取JavaBean对象:</strong><br>取JavaBean对象也很容易，因为JavaBean自身有一些其他属性，所以咱们就可以使用<code>$&#123;对象名.对象属性&#125;</code>或者<code>$&#123;对象名[&#39;对象属性&#39;]&#125;</code>来取值，这和JavaScript语法是不是很相似呢！除此之外，如果该JavaBean如果写了get方法，咱们也可以通过get方法取值例如<code>$&#123;对象.get方法名&#125;</code>完整代码如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>JavaBean对象<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">table</span> <span class="attr">bgcolor</span>=<span class="string">&quot;#ffe4c4&quot;</span> <span class="attr">border</span>=<span class="string">&quot;1&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span>介绍<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;user.name&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span>年龄<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;user[&#x27;age&#x27;]&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span>介绍<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;user.getDetail()&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>运行结果为：<br><img src="/servlet.assets/20200710113504890.png" alt="在这里插入图片描述"><br><strong>取List集合(each):</strong><br>因为List集合是个有序列表，里面内容可能不止一个，你需要遍历List对其中对象取值，而遍历需要用到标签：<code>th:each</code>,具体使用为<code> &lt;tr th:each=&quot;item:$&#123;userlist&#125;&quot;&gt;</code>,其中item就相当于遍历每一次的对象名，在下面的作用域可以直接使用，而userlist就是你在Model中储存的List的名称。完整的代码为：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>List取值<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">table</span> <span class="attr">bgcolor</span>=<span class="string">&quot;#ffe4c4&quot;</span> <span class="attr">border</span>=<span class="string">&quot;1&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tr</span> <span class="attr">th:each</span>=<span class="string">&quot;item:$&#123;userlist&#125;&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;item&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>运行结果为：<br><img src="/servlet.assets/2020071011381727.png" alt="在这里插入图片描述"><br><strong>直接取Map:</strong><br>很多时候我们不存JavaBean而是将一些值放入Map中，再将Map存在Model中，我们就需要对Map取值，对于Map取值你可以<code>$&#123;Map名[&#39;key&#39;]&#125;</code>来进行取值。也可以通过<code>$&#123;Map名.key&#125;</code>取值，当然你也可以使用<code>$&#123;map.get(&#39;key&#39;)&#125;</code>(java语法)来取值，完整代码如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>Map取值<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">table</span> <span class="attr">bgcolor</span>=<span class="string">&quot;#8fbc8f&quot;</span> <span class="attr">border</span>=<span class="string">&quot;1&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span>place:<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;map.get(&#x27;place&#x27;)&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span>&gt;</span>feeling:<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;map[&#x27;feeling&#x27;]&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>运行结果为：<br><img src="/servlet.assets/20200710113846116.png" alt="在这里插入图片描述"><br><strong>遍历Map：</strong><br>如果说你想遍历Map获取它的key和value那也是可以的，这里就要使用和List相似的遍历方法，使用<code>th:each=&quot;item:$&#123;Map名&#125;&quot;</code>进行遍历，在下面只需使用<code>item.key</code>和<code>item.value</code>即可获得值。完整代码如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>Map遍历<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">table</span> <span class="attr">bgcolor</span>=<span class="string">&quot;#ffe4c4&quot;</span> <span class="attr">border</span>=<span class="string">&quot;1&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tr</span> <span class="attr">th:each</span>=<span class="string">&quot;item:$&#123;map&#125;&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;item.key&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">td</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;item.value&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p><img src="/servlet.assets/20200710114011565.png" alt="在这里插入图片描述"></p>
<h5 id="选择变量表达式-…"><a href="#选择变量表达式-…" class="headerlink" title="选择变量表达式: *{…}"></a>选择变量表达式: *{…}</h5><p>变量表达式不仅可以写成${…}，而且还可以写成*{…}。</p>
<p>但是，有一个重要的区别：星号语法对选定对象而不是整个上下文评估表达式。也就是说，只要没有选定的对象，美元(<code>$&#123;…&#125;</code>)和星号(<code>*&#123;...&#125;</code>)的语法就完全一样。</p>
<p>什么是选定对象？使用<code>th:object</code>属性的表达式的结果。就可以选定对象，具体实例如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">th:object</span>=<span class="string">&quot;$&#123;user&#125;&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>Name: <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;*&#123;name&#125;&quot;</span>&gt;</span>赛<span class="tag">&lt;/<span class="name">span</span>&gt;</span>.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>Age: <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;*&#123;age&#125;&quot;</span>&gt;</span>18<span class="tag">&lt;/<span class="name">span</span>&gt;</span>.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>Detail: <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;*&#123;detail&#125;&quot;</span>&gt;</span>好好学习<span class="tag">&lt;/<span class="name">span</span>&gt;</span>.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>当然<code>*&#123;…&#125;</code>也可和<code>$&#123;…&#125;</code>混用。上面的代码如果不使用选定对象，完全等价于：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> &gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>Name: <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;*&#123;user.name&#125;&quot;</span>&gt;</span>赛<span class="tag">&lt;/<span class="name">span</span>&gt;</span>.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>Age: <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;user.age&#125;&quot;</span>&gt;</span>18<span class="tag">&lt;/<span class="name">span</span>&gt;</span>.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">p</span>&gt;</span>Detail: <span class="tag">&lt;<span class="name">span</span> <span class="attr">th:text</span>=<span class="string">&quot;$&#123;user.detail&#125;&quot;</span>&gt;</span>好好学习<span class="tag">&lt;/<span class="name">span</span>&gt;</span>.<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>运行结果为：<br><img src="/servlet.assets/2020070915062122.png" alt="在这里插入图片描述"></p>
<h5 id="消息表达-…"><a href="#消息表达-…" class="headerlink" title="消息表达: #{…}"></a>消息表达: #{…}</h5><p>文本外部化是从模板文件中提取模板代码的片段，以便可以将它们保存在单独的文件(通常是.properties文件)中,文本的外部化片段通常称为“消息”。通俗易懂的来说<code>#&#123;…&#125;</code>语法就是用来<strong>读取配置文件中数据</strong>的。在Thymeleaf你可以使用<code>#&#123;...&#125;</code>语法获取消息，具体实例代码如下：<br>首先在templates目录下建立<code>home.properties</code>中写入以下内容：</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">bigsai.nane</span>=<span class="string">bigsai</span></span><br><span class="line"><span class="attr">bigsai.age</span>=<span class="string">22</span></span><br><span class="line"><span class="attr">province</span>=<span class="string">Jiang Su</span></span><br></pre></td></tr></table></figure>

<p>在<code>application.properties</code>中加入以下内容：</p>
<figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">spring.messages.basename</span>=<span class="string">templates/home</span></span><br></pre></td></tr></table></figure>

<p>这样我们就可以在Thymeleaf中读取配置的文件了，完整代码如下：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">h2</span>&gt;</span>消息表达<span class="tag">&lt;/<span class="name">h2</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">table</span> <span class="attr">bgcolor</span>=<span class="string">&quot;#ffe4c4&quot;</span> <span class="attr">border</span>=<span class="string">&quot;1&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">td</span>&gt;</span>name<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">td</span> <span class="attr">th:text</span>=<span class="string">&quot;#&#123;bigsai.name&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">td</span>&gt;</span>年龄<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">td</span> <span class="attr">th:text</span>=<span class="string">&quot;#&#123;bigsai.age&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">td</span>&gt;</span>province<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">td</span> <span class="attr">th:text</span>=<span class="string">&quot;#&#123;province&#125;&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">table</span>&gt;</span></span><br></pre></td></tr></table></figure>

<p>运行结果为：<br><img src="/servlet.assets/20200709194045973.png" alt="在这里插入图片描述"></p>
<h5 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h5><p>到这里本次Thymeleaf的入门学习就结束了，通过本次学习你可能更深入了解了Thymeleaf以及模板引擎，也对Thymeleaf的动静分离、开箱即用的一些特性估计有了更深入的了解。你也具备Thymeleaf的基本使用能力，能够用Thymeleaf进行网页的快速开发……</p>
<p>但Thymeleaf的内容绝非只有这么一点点，本篇旨在带你从一个对Thymeleaf概念为零的状态到一个能够较为清晰明了的认识和使用Thymeleaf，对于Thymeleaf的内容远远不止上面所涉及到的，对于一些算术运算、条件表达式等等其他内容还需要你自己到Thymeleaf官网去学习研究。</p>
<p>Thymeleaf是一种Java模板引擎，被Springboot官方推荐，大大提高开发效率，提高代码复用率。虽然在当今Ajax更为流行，但对于后端开发工程师掌握Thymeleaf，拥有快速开发网页能力，还是很有必要的！ </p>
<h3 id="9-Servlet保存域"><a href="#9-Servlet保存域" class="headerlink" title="9.Servlet保存域"></a>9.Servlet保存域</h3><p>原始情况下，保存作用域可以认为有四个：</p>
<ul>
<li>page(页面级别)</li>
<li>request(一次请求响应范围)</li>
<li>session(一次会话范围)</li>
<li>application(一次应用程序范围有效)</li>
</ul>
<h3 id="10-路径问题"><a href="#10-路径问题" class="headerlink" title="10.路径问题"></a>10.路径问题</h3><ul>
<li>相对路径：相对路径就是以当前文件为基准进行一级级目录指向被引用的资源文件。</li>
</ul>
<p>​         ..&#x2F;             表示当前文件所在的目录的上一级目录</p>
<p>​         .&#x2F;               表示当前文件所在的目录(可以省略)</p>
<p>​         &#x2F;                表示当前站点的根目录(域名映射的硬盘目录</p>
<ul>
<li>绝对路径：绝对路径就是文件的真正存在的路径，是指从硬盘的根目录(盘符)开始，进行一级级目录指向文件。</li>
</ul>

  </div>
  <div id="copyright"><style>#easter-egg{border:0;padding:10px 0;position:relative}#easter-egg::before{font-family:"Font Awesome 5 Free";font-weight:900;content:"本文结束 \f1b0  感谢阅读";position:absolute;
padding:0 10px;
line-height:1px;
white-space:nowrap;
left:50%;
transform:translateX(-50%)}
</style><hr id="easter-egg"><blockquote style="padding:0">
    <p>作者: <a href="https://mihaoyou.gitee.io">
        <a1>云静</a1>
    </a></p>
    <p>版权声明: 本博客所有文章除特别声明外, 均采用 <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)"><a1>CC BY-NC-ND 4.0</a1></a> 许可协议, 转载请注明出处！</p></blockquote><hr></div>
</article>


    <div class="blog-post-comments">
        <div id="utterances_thread">
            <noscript>加载评论需要在浏览器启用 JavaScript 脚本支持。</noscript>
        </div>
    </div>


        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/about/">关于</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/categories/">分类</a></li>
         
          <li><a href="/search/">搜索</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#Servlet"><span class="toc-number">1.</span> <span class="toc-text">Servlet</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-Servlet%E8%8E%B7%E5%8F%96%E5%8F%82%E6%95%B0"><span class="toc-number">1.1.</span> <span class="toc-text">1.Servlet获取参数</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-1%E7%BC%96%E5%86%99%E9%A1%B5%E9%9D%A2"><span class="toc-number">1.1.1.</span> <span class="toc-text">1.1编写页面</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-2%E5%88%9B%E5%BB%BAservlet%E7%B1%BB"><span class="toc-number">1.1.2.</span> <span class="toc-text">1.2创建servlet类</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-3%E9%85%8D%E7%BD%AEweb-xml"><span class="toc-number">1.1.3.</span> <span class="toc-text">1.3配置web.xml</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#1-4%E5%90%AF%E5%8A%A8tomcat"><span class="toc-number">1.1.4.</span> <span class="toc-text">1.4启动tomcat</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-%E5%A4%84%E7%90%86%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0%E4%B8%AD%E6%96%87%E4%B9%B1%E7%A0%81%E9%97%AE%E9%A2%98"><span class="toc-number">1.2.</span> <span class="toc-text">2.处理请求参数中文乱码问题</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-Servlet%E7%9A%84%E7%BB%A7%E6%89%BF%E5%85%B3%E7%B3%BB"><span class="toc-number">1.3.</span> <span class="toc-text">3.Servlet的继承关系</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-Servlet%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-number">1.4.</span> <span class="toc-text">4.Servlet生命周期</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-Http%E5%8D%8F%E8%AE%AE"><span class="toc-number">1.5.</span> <span class="toc-text">5.Http协议</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#5-1Http%E5%8D%8F%E8%AE%AE%E7%AE%80%E4%BB%8B"><span class="toc-number">1.5.1.</span> <span class="toc-text">5.1Http协议简介</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-2HTTP-%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86"><span class="toc-number">1.5.2.</span> <span class="toc-text">5.2HTTP 工作原理</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-3%E8%AF%B7%E6%B1%82%E6%96%B9%E6%B3%95"><span class="toc-number">1.5.3.</span> <span class="toc-text">5.3请求方法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-4%E8%AF%B7%E6%B1%82%E6%B6%88%E6%81%AF%E5%A4%B4"><span class="toc-number">1.5.4.</span> <span class="toc-text">5.4请求消息头</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-5%E8%AF%B7%E6%B1%82%E4%BD%93"><span class="toc-number">1.5.5.</span> <span class="toc-text">5.5请求体</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-6%E5%93%8D%E5%BA%94"><span class="toc-number">1.5.6.</span> <span class="toc-text">5.6响应</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-7%E7%8A%B6%E6%80%81%E7%A0%81"><span class="toc-number">1.5.7.</span> <span class="toc-text">5.7状态码</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-%E4%BC%9A%E8%AF%9D"><span class="toc-number">1.6.</span> <span class="toc-text">6.会话</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#6-1HTTP%E6%98%AF%E6%97%A0%E7%8A%B6%E6%80%81%E7%9A%84"><span class="toc-number">1.6.1.</span> <span class="toc-text">6.1HTTP是无状态的</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-2%E4%BC%9A%E8%AF%9D%E8%B7%9F%E8%B8%AA%E6%8A%80%E6%9C%AF"><span class="toc-number">1.6.2.</span> <span class="toc-text">6.2会话跟踪技术</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-3session%E4%BF%9D%E5%AD%98%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">1.6.3.</span> <span class="toc-text">6.3session保存作用域</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7-%E6%9C%8D%E5%8A%A1%E5%99%A8%E5%86%85%E9%83%A8%E8%BD%AC%E5%8F%91%E4%BB%A5%E5%8F%8A%E5%AE%A2%E6%9C%8D%E7%AB%AF%E9%87%8D%E5%AE%9A%E5%90%91"><span class="toc-number">1.7.</span> <span class="toc-text">7.服务器内部转发以及客服端重定向</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8-Thymeleaf%E2%80%93%E8%A7%86%E5%9B%BE%E6%A8%A1%E7%89%88%E6%8A%80%E6%9C%AF"><span class="toc-number">1.8.</span> <span class="toc-text">8.Thymeleaf–视图模版技术</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%A6%E4%B9%A0Thymeleaf%E5%BF%85%E7%9F%A5%E7%9A%84%E7%9F%A5%E8%AF%86%E7%82%B9"><span class="toc-number">1.8.1.</span> <span class="toc-text">学习Thymeleaf必知的知识点</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Springboot"><span class="toc-number">1.8.2.</span> <span class="toc-text">Springboot</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#MVC%E4%BB%8B%E7%BB%8D"><span class="toc-number">1.8.3.</span> <span class="toc-text">MVC介绍</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-1%E5%8A%A8%E9%9D%99%E5%88%86%E7%A6%BB"><span class="toc-number">1.8.4.</span> <span class="toc-text">8.1动静分离</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-2-%E5%88%9B%E5%BB%BA%E7%AC%AC%E4%B8%80%E4%B8%AAthymeleaf%E9%A1%B5%E9%9D%A2"><span class="toc-number">1.8.5.</span> <span class="toc-text">8.2 创建第一个thymeleaf页面</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%B8%B8%E7%94%A8%E6%A0%87%E7%AD%BE"><span class="toc-number">1.8.5.1.</span> <span class="toc-text">常用标签</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%93%BE%E6%8E%A5%E8%A1%A8%E8%BE%BE%E5%BC%8F-%E2%80%A6"><span class="toc-number">1.8.5.2.</span> <span class="toc-text">链接表达式: @{…}</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E8%A1%A8%E8%BE%BE%E5%BC%8F-%E2%80%A6"><span class="toc-number">1.8.5.3.</span> <span class="toc-text">变量表达式: ${…}</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%80%89%E6%8B%A9%E5%8F%98%E9%87%8F%E8%A1%A8%E8%BE%BE%E5%BC%8F-%E2%80%A6"><span class="toc-number">1.8.5.4.</span> <span class="toc-text">选择变量表达式: *{…}</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%B6%88%E6%81%AF%E8%A1%A8%E8%BE%BE-%E2%80%A6"><span class="toc-number">1.8.5.5.</span> <span class="toc-text">消息表达: #{…}</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">1.8.5.6.</span> <span class="toc-text">总结</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#9-Servlet%E4%BF%9D%E5%AD%98%E5%9F%9F"><span class="toc-number">1.9.</span> <span class="toc-text">9.Servlet保存域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#10-%E8%B7%AF%E5%BE%84%E9%97%AE%E9%A2%98"><span class="toc-number">1.10.</span> <span class="toc-text">10.路径问题</span></a></li></ol></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.facebook.com/sharer.php?u=https://mihaoyou.gitee.io/servlet/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://twitter.com/share?url=https://mihaoyou.gitee.io/servlet/&text=servlet"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.linkedin.com/shareArticle?url=https://mihaoyou.gitee.io/servlet/&title=servlet"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://pinterest.com/pin/create/bookmarklet/?url=https://mihaoyou.gitee.io/servlet/&is_video=false&description=servlet"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=servlet&body=Check out this article: https://mihaoyou.gitee.io/servlet/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://getpocket.com/save?url=https://mihaoyou.gitee.io/servlet/&title=servlet"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://reddit.com/submit?url=https://mihaoyou.gitee.io/servlet/&title=servlet"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.stumbleupon.com/submit?url=https://mihaoyou.gitee.io/servlet/&title=servlet"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://digg.com/submit?url=https://mihaoyou.gitee.io/servlet/&title=servlet"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="http://www.tumblr.com/share/link?url=https://mihaoyou.gitee.io/servlet/&name=servlet&description="><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" target="_blank" rel="noopener" href="https://news.ycombinator.com/submitlink?u=https://mihaoyou.gitee.io/servlet/&t=servlet"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
      <div class="footer-left">
        Copyright &copy;
        
        
        2022-2023
        云雪
      </div>
      <div class="footer-right">
        <nav>
          <ul>
            <!--
          --><li><a href="/">首页</a></li><!--
        --><!--
          --><li><a href="/about/">关于</a></li><!--
        --><!--
          --><li><a href="/archives/">归档</a></li><!--
        --><!--
          --><li><a href="/categories/">分类</a></li><!--
        --><!--
          --><li><a href="/search/">搜索</a></li><!--
        -->
          </ul>
          <ul>
          
              <!-- 不蒜子统计 -->
              <span id="busuanzi_container_site_pv">
                  本站总访问量<span id="busuanzi_value_site_pv"></span>次
              </span>
              <span class="post-meta-divider">|</span>
              <span id="busuanzi_container_site_uv" style='display:none'>
                      本站访客数<span id="busuanzi_value_site_uv"></span>人
              </span>
            <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            
          </ul>
        </nav>
      </div>
      
</footer>


    </div>
    <!-- styles -->



  <link rel="preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" crossorigin="anonymous" onload="this.onload=null;this.rel='stylesheet'"/>


    <!-- jquery -->
 
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script> 




<!-- clipboard -->

  
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.7/clipboard.min.js" crossorigin="anonymous"></script> 
  
  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"复制到粘贴板！\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "复制成功！");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

  <script type="text/javascript">
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?d052d72734717a430b46e4d07257350b";
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(hm, s);
        })();
        </script>

<!-- Cloudflare Analytics -->

<!-- Umami Analytics -->

<!-- Disqus Comments -->

<!-- utterances Comments -->

    <script type="text/javascript">
      var utterances_repo = 'mihaoyou/huayu';
      var utterances_issue_term = 'pathname';
      var utterances_label = 'Comment';
      var utterances_theme = 'github-dark';

      (function(){
          var script = document.createElement('script');

          script.src = 'https://utteranc.es/client.js';
          script.setAttribute('repo', utterances_repo);
          script.setAttribute('issue-term', 'pathname');
          script.setAttribute('label', utterances_label);
          script.setAttribute('theme', utterances_theme);
          script.setAttribute('crossorigin', 'anonymous');
          script.async = true;
          (document.getElementById('utterances_thread')).appendChild(script);
      }());
  </script>

</body>
</html>
